package com.notepad.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.util.ArrayList;
import java.util.List;

import com.notepad.R;
import com.notepad.domain.NoteEntity;
import com.notepad.domain.UserSetting;
import com.notepad.ui.EmptyViewTipActivity;
import com.notepad.ui.WriteNewNoteActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.sax.StartElementListener;
import android.widget.Toast;

public class FileUtil {
	private File mSDCard;
	private File mAppDir;
	public File mSaveUserSettingFile;
	//private NoteEntity mNoteEntity;
    private UserSetting mUserSetting;

	
	public FileUtil(Context context) {
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			mSDCard = Environment.getExternalStorageDirectory();
			mAppDir = new File(mSDCard, Constant.USER_SETTING_FILE);
			if (!mAppDir.exists()) {
				mAppDir.mkdir();
			}

		}else{
			Toast.makeText(context, R.string.no_sdcard_tip, Toast.LENGTH_LONG).show();
		}
        mSaveUserSettingFile = new File(mAppDir, Constant.SAVE_LOCK_INFO_FILE);
	}

	public void clearFile(){
		if(!mSaveUserSettingFile.exists()){
			try {
                mSaveUserSettingFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
            mSaveUserSettingFile.delete();
			try {
                mSaveUserSettingFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	/*public ArrayList<NoteEntity> getNoteList(){
		
		List<NoteEntity> list = 
				SerializableUtil.unserializList(mSaveNoteFile);
		if(list.isEmpty()){
			list = new ArrayList<NoteEntity>();
		}
		return (ArrayList<NoteEntity>) list;
	}*/

	/*public NoteEntity getNoteObject(){
		NoteEntity noteEntity = (NoteEntity) SerializableUtil.unserializObject(mSaveNoteFile);
		return noteEntity;
	}
*/
    public UserSetting getUserSetting() {
        UserSetting userSetting = (UserSetting) SerializableUtil.unserializObject(mSaveUserSettingFile);
        return userSetting;
    }

    public void SaveUserSetting(Object obj){
            SerializableUtil.serializObject(obj, mSaveUserSettingFile);
    }
	/*public void saveNoteObject(Object obj){
		SerializableUtil.serializObject(obj, mSaveNoteFile);	
	}
	public void saveNoteList(List<NoteEntity> list){
		SerializableUtil.serializList(list, mSaveNoteFile);
	}*/
	
}
